package top.starcatmeow.encryptedchat;

import android.util.Log;
import android.view.View;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ActionProcesser {
    Socket socket = null;
    Thread messagehandlerThread = null;
    DataOutputStream dos = null;

    public void connect(String ip, String port, String username, String password) {
        Log.i("aaa", ip + port + username + password);
        boolean b1 = true;
        while (b1) {
            try {
                socket = new Socket(ip, new Integer(port));                                                         //向服务器发起Socket请求
                b1 = false;
            } catch (IOException e1) {
                //进入此处代表无法连接到服务器
                e1.printStackTrace();                                                                               //输出错误信息至控制台
                return;                                                                                             //停止后续操作
            } catch (NumberFormatException e1) {
                //进入此处代表用户输入的服务器端口无法正常转换为数字
                e1.printStackTrace();                                                                               //输出错误信息至控制台
                return;                                                                                             //停止后续操作
            }
        }

        try {
            Cert.getAESKey(socket, username, password);
        } catch (Exception e1) {
            e1.printStackTrace();
            return;                                                                                                 //停止后续操作
        }

        messagehandlerThread = new Thread(new MessageHandler(socket));                                         //新建接收线程
        messagehandlerThread.start();                                                                          //启动线程


        try {
            dos = new DataOutputStream(socket.getOutputStream());                                                   //新建输出流
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        ChatActivity.sendbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ChatActivity.text.getText().toString().equals("")) {
                    //输入框不为空进入此处
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String tempstr = null;
                                tempstr = AES.getInstance().encrypt(ChatActivity.text.getText().toString());
                                System.out.println(tempstr);
                                dos.writeUTF(tempstr);                                //加密后发送至服务
                                ChatActivity.text.setText("");                                                                            //清空输入框
                            } catch (IOException e1) {
                                e1.printStackTrace();                                                                       //显示错误信息
                            }
                        }
                    }).start();

                }
            }
        });
    }

    public void disconnect() {
        messagehandlerThread.interrupt();

        try {
            socket.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }
/*            int clear = JOptionPane.showConfirmDialog(null, getUIString.get("emptymessages"), getUIString.get("clear"), JOptionPane.YES_NO_OPTION);
            if (clear == 0) {
                ChatActivity.tv.setText("");
            }*/
    }

}
